<?php 

$id = htmlentities(mysql_real_escape_string($_GET['id']));

$db = New User();
$table   = "user";
$table2 = "pegawai";

$pegawai 	= $db->lihat_pegawai($table2);
$user    	= $db->lihat_user($table,$id);

if ($user) {
		
		$o .= '
				<form class="form-horizontal" id="registerHere" method="post" action="?page=edituser&action=edit">
				  <fieldset>
					<legend>EDIT USER</legend>';
	  
		if (isset($_GET['success']) && ($_GET['success'] == "1")) {

			$o .= '<div class="alert alert-success">
					Proses edit user berhasil
					</div>';
					
		} elseif (isset($_GET['success']) && ($_GET['success'] == "0")) {
			
			$o .= '<div class="alert alert-error">
					Proses edit user gagal
				   </div>';
		}
		
		
		foreach ($user as $x) {
			
			$o .= '<div class="control-group">
					<label class="control-label" for="input01">Nama</label>
						<div class="controls">
							<select name="name" id="name" class="chzn-select" style="width:280px;" tabindex="2">
							<option value='.$x['idpegawai'].'>'.$x['nama_pegawai'].'</option>';		   
							if ($pegawai) {
								foreach ($pegawai as $r) {
									$o .= '<option value='.$r['idpegawai'].'>'.$r['nama_pegawai'].'</option>';
								}
							} else {
								$o .= '<option value="">tidak ada nama pegawai</option>';
							}
							
			$o .= '</select>	
					</div>
						</div>
						
						<div class="control-group">
						  <label class="control-label" for="input01">Username</label>
						  <div class="controls">
							<input type="text" class="input-xlarge" id="username" value='.$x['username'].' name="username" rel="popover" data-content="Masukkan username." data-original-title="Username" readonly=readonly>
						  </div>
						</div>

					 <div class="control-group">
						<label class="control-label" for="input01">Email</label>
						  <div class="controls">
							<input type="text" class="input-xlarge" id="email" value='.$x['email'].' name="email" rel="popover" data-content="Masukkan alamat email" data-original-title="Email">
						   
						  </div>
					</div>

					<div class="control-group">
						<label class="control-label" for="input01">Password</label>
						  <div class="controls">
							<input type="password" class="input-xlarge" id="password" name="password" rel="popover" data-content="6 characters or more! Be tricky" data-original-title="Password" >       
						  </div>
					</div>

					<div class="control-group">
						<label class="control-label" for="input01">Konfirmasi Password</label>
						  <div class="controls">
							<input type="password" class="input-xlarge" id="confirmpass" name="confirmpass" rel="popover" data-content="Re-enter your password for confirmation." data-original-title="Re-Password" >
						  </div>
					</div>

					<div class="control-group">
						<label class="control-label" for="input01"></label>
						  <div class="controls">
						   <button type="submit" class="btn btn-success" rel="tooltip" title="first tooltip">Edit User</button>
						  </div>
					</div>
					  </fieldset>
					</form>';		
		
		}
		
} else {
	$o .= '<div class="alert alert-error">
			User tidak ditemukan
			</div>';
}
		
echo $o;
		
?>
			
	<a href="?page=user">kembali</a>

<?php


if ($_GET['action'] == "edit") {
	
	$name 			= htmlentities(mysql_real_escape_string($_POST['name']));
	$username	= htmlentities(mysql_real_escape_string($_POST['username']));
	$email			= htmlentities(mysql_real_escape_string($_POST['email']));
	$pass			= htmlentities(mysql_real_escape_string($_POST['password']));
	$level			= "Operator";
	
	$db = new User();
	$user = array('password'=>$pass, 'idpegawai'=>$name, 'email'=>$email, 'hak_akses'=>$level, 'idsektoral'=>$sektoral);	
	$edituser = $db->ubah($table, $user,$username);
	if ($edituser) {
		echo '
			<script language="javascript">
			document.location="?page=edituser&id='.$username.'success=1";
			</script>';
		
	} else {
		echo '
			<script language="javascript">
			document.location="?page=edituser&id='.$username.'&success=0";
			</script>';
	
	}
	
}





